﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;
using Galaktika.BI.Silverlight.Services;
using Galaktika.BI.Extensibility.Dom.Dac;
using Ranet.Olap.Core.Data;
using Ranet.AgOlap.Controls.MemberChoice.ClientServer;
//using Ranet.Web.Olap;
using Ranet.Olap.Core;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands.MemberChoice
{
    public class MemberChoiceCommand : Galaktika.BI.Silverlight.Commands.ServerCommand
    {
        public MemberChoiceCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.MemberChoiceCommandId)
        {
            this.Schema = invoker;
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.ConnectionId = invoker[KnownInvokeArgs.ConnectionId];
            this.Args = invoker[KnownInvokeArgs.MemberChoiceQueryInfo];
        }

        public InvokeSchema Schema { get; private set; }
        public string ContextId { get; private set; }
        public string ConnectionId { get; private set; }
        public string Args { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

//        OlapDataManager m_Manager = null;
//        private OlapDataManager DataManager
//        {
//            get
//            {
//                if (m_Manager == null)
//                {
//                    BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
//                    if (conn != null)
//                    {
//#warning DataSourceId?
//                        m_Manager = new OlapDataManager("", new ConnectionInfo(conn.Name, conn.ConnectionString), String.Empty, String.Empty);
//                    }
//                }
//                return m_Manager;
//            }
//        }

        public override InvokeResultSchema Invoke()
        {
            MemberChoiceQuery args = XmlUtility.XmlStr2Obj<MemberChoiceQuery>(this.Args);

            BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
            string connStr = null;
            string subCube = args.SubCube;
            if (conn != null && this.Context != null)
            {
                var exe = (IExecutionContext )this.Context.Services.GetService(typeof(IExecutionContext));
                if (exe != null)
                {
                    connStr = exe.Parse(this.Context, conn.ConnectionString);
                    subCube = exe.Parse(this.Context, args.SubCube);
                }
            }
            args.Connection = connStr;
            args.SubCube = subCube;
            this.Schema[KnownInvokeArgs.MemberChoiceQueryInfo] = XmlUtility.Obj2XmlStr(args);

            using (var srv = new SyncDataService())
            {
                return srv.InvokeCommand(this.Schema);
            }
            //if (DataManager != null)
            //{
            //    switch (args.QueryType)
            //    {
            //        case MemberChoiceQueryType.GetRootMembersCount:
            //            return GetRootMembersCount(args);
            //        case MemberChoiceQueryType.GetRootMembers:
            //            return GetRootMembers(args);
            //        case MemberChoiceQueryType.GetChildrenMembers:
            //            return GetChildrenMembers(args);
            //        case MemberChoiceQueryType.FindMembers:
            //            return FindMembers(args);
            //        case MemberChoiceQueryType.GetAscendants:
            //            return GetAscendants(args);
            //        case MemberChoiceQueryType.GetMember:
            //            return GetMember(args);
            //        case MemberChoiceQueryType.GetMembers:
            //            return GetMembers(args);
            //        case MemberChoiceQueryType.LoadSetWithAscendants:
            //            return LoadSetWithAscendants(args);
            //    }
            //}

            //res.ContentType = KnownContentTypes.MultidimData;
            //return res;
        }

        /*
        InvokeResultSchema LoadSetWithAscendants(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //members = provider.LoadSetWithAscendants(args.Set, args.CubeName, args.HierarchyUniqueName);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);
            return res;
        }

        InvokeResultSchema GetMembers(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //members = provider.GetMembers(args.CubeName, args.Set);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);
            return res;
        }

        InvokeResultSchema GetMember(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //MemberDataWrapper member = null;
            //member = provider.GetMember(args.CubeName, args.MemberUniqueName, args.Properties);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(member, Common.Namespace);
            return res;
        }

        InvokeResultSchema GetRootMembersCount(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //double count = 0;

            //if (String.IsNullOrEmpty(args.StartLevelUniqueName))
            //    count = provider.GetMembersCount(args.CubeName, args.HierarchyUniqueName, 0);
            //else
            //    count = provider.GetMembersCount(args.CubeName, args.HierarchyUniqueName, args.StartLevelUniqueName);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = count.ToString();

            return res;
        }

        InvokeResultSchema GetRootMembers(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //if (String.IsNullOrEmpty(args.StartLevelUniqueName))
            //    members = provider.GetHierarchyMembers(args.CubeName, args.HierarchyUniqueName, 0, args.BeginIndex, args.Count);
            //else
            //    members = provider.GetLevelMembers(args.CubeName, args.HierarchyUniqueName, args.StartLevelUniqueName, args.BeginIndex, args.Count);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);

            return res;
        }

        InvokeResultSchema GetChildrenMembers(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //members = provider.GetChildrenMembers(args.CubeName, args.HierarchyUniqueName, args.MemberUniqueName, args.BeginIndex, args.Count);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);

            return res;
        }

        InvokeResultSchema FindMembers(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //members = provider.SearchMembers(args.CubeName, args.HierarchyUniqueName, args.StartLevelUniqueName, args.Filter);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);

            return res;
        }

        InvokeResultSchema GetAscendants(MemberChoiceQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            //OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            //List<MemberDataWrapper> members = null;
            //members = provider.GetAscendants(args.CubeName, args.HierarchyUniqueName, args.StartLevelUniqueName, args.MemberUniqueName);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);

            return res;
        }
        */
    }
}
